home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / PowerPC / pdflib / test / pdftest.c < prev    next >
C/C++ Source or Header  |  2000-05-16  |  25KB  |  988 lines

  1. /*---------------------------------------------------------------------------*
  2.  |        PDFlib - A library for dynamically generating PDF files            |
  3.  +---------------------------------------------------------------------------+
  4.  |        Copyright (c) 1997-1999 Thomas Merz. All rights reserved.          |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is not in the public domain.  It is subject to the       |
  7.  |    "Aladdin Free Public License".  See the file license.txt for details.  |
  8.  |    This license grants you the right to use and redistribute PDFlib       |
  9.  |    under certain conditions. Among other things, the license requires     |
  10.  |    that the copyright notice and this notice be preserved on all copies.  |
  11.  |    This requirement extends to ports to other programming languages.      |
  12.  |                                                                           |
  13.  |    In short, you are allowed to develop and use PDFlib-based software     |
  14.  |    as long as you don't sell it. Commercial use of PDFlib requires a      |
  15.  |    commercial license which can be obtained from the author of PDFlib.    |
  16.  |    Contact information can be found in the accompanying PDFlib manual.    |
  17.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  18.  |    however, will receive warranty and support statements in writing.      |
  19.  *---------------------------------------------------------------------------*/
  20.  
  21. /* pdftest.c
  22.  *
  23.  * Test bed and sample application for PDFlib
  24.  */
  25.  
  26. #ifdef WIN32
  27.     /* Visual C++ seems to need this in order to fetch the thread functions */
  28.     #ifndef _MT
  29.     #define _MT
  30.     #endif
  31.  
  32.     #include <windows.h>
  33.     #include <process.h>
  34. #endif    /* WIN32 */
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <math.h>
  40.  
  41. #include "pdflib.h"
  42.  
  43. #if __POWERPC__ || __CFM68K__ || __MC68K__
  44. #define MAC
  45. #endif
  46.  
  47. /* Don't clutter output from many threads with messages */
  48. #ifdef WIN32
  49. #define MESSAGE(m)        /* */
  50. #define PDF_ENDTHREAD(val)    _endthreadex(val);
  51. #else
  52. #define MESSAGE(m)        fprintf(stderr, m)
  53. #define PDF_ENDTHREAD(val)    /* */
  54. #endif
  55.  
  56. /* ------------------------------------------------------------- */
  57. static void
  58. bookmarks(PDF *p)
  59. {
  60. #define UNICODEINFO    "\xFE\xFF\0001\000.\000A\0\0"
  61. #define LEFT 50
  62. #define FONTSIZE 24
  63. #define LEAD ((int) (FONTSIZE * 1.5))
  64.  
  65.     unsigned char buf[64], tmp[64];
  66.     int c, i, pos;
  67.     float y=700;
  68.     int level1, level2=0, level3=0, font;
  69.  
  70.     MESSAGE("Bookmark test...");
  71.  
  72.     font = PDF_findfont(p, "Helvetica", "default", 0);
  73.     if (font == -1) {
  74.     fprintf(stderr, "\nFont %s not found!\n", "Helvetica");
  75.     return;
  76.     }
  77.  
  78.     PDF_begin_page(p, a4_width, a4_height);
  79.  
  80.     PDF_setfont(p, font, FONTSIZE);
  81.  
  82.     PDF_show_xy(p, "The bookmarks for this page contain all", LEFT, y-=LEAD);
  83.     PDF_show_xy(p, "Unicode characters.", LEFT, y-=LEAD);
  84.     PDF_show_xy(p, "Depending on the available fonts,", LEFT, y-=LEAD);
  85.     PDF_show_xy(p, "only a certain subset will be visible on", LEFT, y-=LEAD);
  86.     PDF_show_xy(p, "your system.", LEFT, y-=LEAD);
  87.     
  88.     /* private Unicode info entry */
  89.     PDF_set_info(p, "Revision", UNICODEINFO);
  90.  
  91.     /* Generate Unicode bookmarks */
  92.     level1 = PDF_add_bookmark(p, "Unicode bookmarks", 0, 0);
  93.  
  94.     for (c = 0; c < 65535; c += 16) {
  95.     if (c % 4096 == 0) {
  96.         sprintf((char *) tmp, "U+%04X", c);
  97.         level2 = PDF_add_bookmark(p, (char *) tmp, level1, 0);
  98.     }
  99.     if (c % 256 == 0) {
  100.         sprintf((char *) tmp, "U+%04X", c);
  101.         level3 = PDF_add_bookmark(p, (char *) tmp, level2, 0);
  102.     }
  103.     /* now comes the actual Unicode string with the BOM */
  104.     sprintf((char *) tmp, "0x%04X: ", c);
  105.  
  106.     /* write the Unicode byte order mark */
  107.     strcpy((char *) buf, "\376\377");
  108.  
  109.     for (pos = 0; tmp[pos/2]; /* */ ) {
  110.         buf[2+pos++] = 0;
  111.         buf[2+pos++] = tmp[pos/2];    /* construct Unicode string */
  112.     }
  113.  
  114.     pos += 2;     /* account for the BOM */
  115.     for (i = 0; i < 16; i++) {    /* continue filling buf with chars */
  116.         buf[pos++] = (unsigned char) ((((c+i)) & 0xFF00) >> 8);
  117.         buf[pos++] = (unsigned char) (((c+i)) & 0x00FF);
  118.     }
  119.  
  120.     /* signal end of string with two null bytes */
  121.     buf[pos++] = (unsigned char) 0;
  122.     buf[pos++] = (unsigned char) 0;
  123.  
  124.     (void) PDF_add_bookmark(p, (char *) buf, level3, 1);
  125.     }
  126.  
  127.     PDF_end_page(p);
  128.  
  129.     MESSAGE("done\n");
  130.  
  131. #undef FONTSIZE
  132. #undef LEFT
  133. #undef UNICODEINFO
  134. #undef LEAD
  135. }
  136.  
  137. /* ------------------------------------------------------------- */
  138. static void
  139. ccitt_image(PDF *p)
  140. {
  141.     int    image;
  142.     float    sx, sy;
  143.  
  144. #define CCITTFILE    "tm.g3"
  145. #define WIDTH        591
  146. #define HEIGHT        236
  147.  
  148. /* Scale to non-standard fax resolutions */
  149. #define FAX_DPI_X    72
  150. #define FAX_DPI_Y    72
  151.  
  152.     sx = (float) (72.0 / FAX_DPI_X);
  153.     sy = (float) (72.0 / FAX_DPI_Y);
  154.  
  155.     MESSAGE("CCITT test...");
  156.  
  157.     if ((image = PDF_open_CCITT(p, CCITTFILE, WIDTH, HEIGHT, 0, 0, 0)) == -1) {
  158.     fprintf(stderr, "Error: Couldn't open CCITT image %s.\n", CCITTFILE);
  159.     return;
  160.     }
  161.  
  162.     PDF_begin_page(p, (float) PDF_get_image_width(p, image) * sx,
  163.             (float) PDF_get_image_height(p, image) * sy);
  164.  
  165.     (void) PDF_add_bookmark(p, "CCITT image", 0, 1);
  166.  
  167.     PDF_scale(p, sx, sy);
  168.     PDF_place_image(p, image, (float) 0.0, (float) 0.0, (float) 1.0);
  169.  
  170.     PDF_end_page(p);
  171.  
  172.     MESSAGE("done\n");
  173.  
  174. #undef CCITTFILE
  175. }
  176.  
  177. /* ------------------------------------------------------------- */
  178. static void
  179. gif_image(PDF *p)
  180. {
  181.     int    image;
  182.  
  183. #define GIFFILE        "pdflib.gif"
  184.  
  185.     MESSAGE("GIF test...");
  186.  
  187.     if ((image = PDF_open_GIF(p, GIFFILE)) == -1) {
  188.     fprintf(stderr, "Error: Couldn't analyze GIF image %s.\n", GIFFILE);
  189.     return;
  190.     }
  191.  
  192.     PDF_begin_page(p, (float) PDF_get_image_width(p, image),
  193.                 (float) PDF_get_image_height(p, image));
  194.     (void) PDF_add_bookmark(p, "GIF image", 0, 1);
  195.  
  196.     PDF_place_image(p, image, (float) 0.0, (float) 0.0, (float) 1.0);
  197.  
  198.     PDF_end_page(p);
  199.  
  200.     MESSAGE("done\n");
  201.  
  202. #undef GIFFILE
  203. }
  204.  
  205. /* ------------------------------------------------------------- */
  206. #ifdef HAVE_LIBTIFF
  207. static void
  208. tiff_image(PDF *p)
  209. {
  210.     int    image;
  211.  
  212. #define TIFFFILE    "acroweb_j.tif"
  213.  
  214.     MESSAGE("TIFF test...");
  215.  
  216.     if ((image = PDF_open_TIFF(p, TIFFFILE)) == -1) {
  217.     fprintf(stderr, "Error: Couldn't analyze TIFF image %s.\n", TIFFFILE);
  218.     return;
  219.     }
  220.  
  221.     PDF_begin_page(p, PDF_get_image_width(p, image), PDF_get_image_height(p, image));
  222.     (void) PDF_add_bookmark(p, "TIFF image", 0, 1);
  223.  
  224.     PDF_place_image(p, image, 0.0, 0.0, 1.0);
  225.  
  226.     PDF_end_page(p);
  227.  
  228.     MESSAGE("done\n");
  229.  
  230. #undef TIFFFILE
  231. }
  232. #endif
  233.  
  234. /* ------------------------------------------------------------- */
  235. static void
  236. jpeg_image(PDF *p)
  237. {
  238.     int    image;
  239.     float    scale;
  240.  
  241. #define JPEGFILE    "nesrin.jpg"
  242.  
  243.     MESSAGE("JPEG test...");
  244.  
  245.     if ((image = PDF_open_JPEG(p, JPEGFILE)) == -1) {
  246.     fprintf(stderr, "Error: Couldn't analyze JPEG image %s.\n", JPEGFILE);
  247.     return;
  248.     }
  249.  
  250.     PDF_begin_page(p, a4_width, a4_height);
  251.     (void) PDF_add_bookmark(p, "JPEG image", 0, 1);
  252.  
  253.     /* ----------------- first image ------------------- */
  254.     /* fit image to page width */
  255.     scale = (float) a4_width/PDF_get_image_width(p, image);
  256.     PDF_place_image(p, image, (float) 0.0, 
  257.         a4_height - PDF_get_image_height(p, image) * scale, scale);
  258.  
  259.     /* ----------------- second image ------------------- */
  260.     scale = (float) 0.5;
  261.     PDF_save(p);
  262.     PDF_rotate(p, (float) 90.0);
  263.     PDF_place_image(p, image, (float) 0, (float) (-1 * a4_width), scale);
  264.     PDF_restore(p);
  265.  
  266.     /* ----------------- third image ------------------- */
  267.     scale = (float) 0.25;
  268.     PDF_save(p);
  269.     PDF_rotate(p, (float) 45.0);
  270.     PDF_place_image(p, image, (float) 200, (float) 0.0, scale);
  271.     PDF_restore(p);
  272.  
  273.     PDF_end_page(p);
  274.  
  275.     MESSAGE("done\n");
  276.  
  277. #undef JPEGFILE
  278. }
  279.  
  280. /* ------------------------------------------------------------- */
  281. static void
  282. character_table(PDF *p)
  283. {
  284.     char    text[50];
  285.     int        i, j, font;
  286.     float    x, y;
  287.  
  288. #define LEFT        50
  289. #define TOP        700
  290. #define FONTSIZE    16
  291. #define FONTNAME    "Times-Roman"
  292.  
  293. #ifdef MAC
  294.     #define ENCODING    "macroman"
  295. #else
  296.     #define ENCODING    "winansi"
  297. #endif
  298.  
  299.     MESSAGE("Character encoding test...");
  300.  
  301.     font = PDF_findfont(p, FONTNAME, ENCODING, 1);
  302.     if (font == -1) {
  303.     fprintf(stderr, "Fon